import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {BaseGoodsCondition} from '../../../../goods-management/goo-base-goods/base-goods.type';
import {GooBaseGoodsSpecificationCondition} from '../../../../goods-management/goo-base-goods-specification/goo-base-goods-specification.type';
import {BsModalService} from 'ngx-bootstrap';
import {Services} from '../../../../../frame/module/services';
import {isNullOrUndefined} from "util";
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {WmsWarehouseManageCondition} from '../../../wms-warehouse-manage/wms-warehouse-manage.type';
import {WmsStockTransfer, WmsStockTransferCondition} from '../../wms-stock-transfer.type';
import {WmsStockDetail} from '../../../wms-stock-detail/wms-stock-detail.type';
import * as fa from '@fortawesome/free-solid-svg-icons';

@Component({
  selector: 'app-wms-select-stock-goods',
  templateUrl: './wms-select-stock-goods.component.html',
  styleUrls: ['./wms-select-stock-goods.component.css']
})
export class WmsSelectStockGoodsComponent implements OnInit {

    utils = {
        buildGoodsData: ( goodsItem, goodsSpecificationItem  ) => {
            return {
                goodsUUID: goodsItem.uuid, // 商品ID
                goodsName: goodsItem.serGoodsName, // 商品名称
                goodsSerNum: goodsItem.serNum, // 商品编号
                goodsSpecificationUUID: goodsSpecificationItem.id, // 商品规格ID
                goodsSpecificationName: goodsSpecificationItem.name, // 规格名称
            };
        }
    };
    // 商品查询条件
    condition = new BaseGoodsCondition();
    // 商品规格查询条件
    guiGeCondition = new GooBaseGoodsSpecificationCondition();


    // 表单数据
    formGuiGe = [];
    searchFlag = false;
    selectedGuiGe;
    // 产品规格
    specTypes = [];
    // 商品列表数据
    goodsData;
    // 选择的商品
    selectedGoods;
    // 模拟查询商品的数据
    wmsStockDetailMap = new Map<string, WmsStockDetail>();
    constructor(private modalService: BsModalService) {
    }

    @ViewChild('lgModal')
    lgModal;
    @Output()
    saveOrder = new EventEmitter<any>();

    domain = new WmsStockTransfer();
    extend = {
        domain: {
            outWarehouseName: {
                items: [],
                nickname: '调出仓库名称',
                change: (event) => {
                    console.log(event);
                    this.domain.outWarehouseName = event.name;
                    this.domain.outWarehouseUuid = event.id;
                }
            }
        },
    };
    head = {
        tr: {
            selectedAll: {
                icon: fa.faCheckCircle,
                click: (event) => {
                    if (this.body.tr.selectedUuid.size === 0) {
                        this.data.page.content.forEach(item => {
                            this.body.tr.selectedUuid.set(item.uuid, item.uuid);
                        });
                        this.head.tr.selectedAll.icon = fa.faCheck;
                    } else {
                        this.body.tr.selectedUuid.clear();
                        this.head.tr.selectedAll.icon = fa.faCheckCircle;
                    }
                }
            }
        },
    };
    body = {
        tr: {
            selectedUuid: new Map(),
            isSelected: (uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                return !isNullOrUndefined(value);
            },
            click: (event, uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                if (isNullOrUndefined(value)) {
                    this.body.tr.selectedUuid.set(uuid, '0');
                } else {
                    this.body.tr.selectedUuid.delete(uuid);
                }
            }
        }
    };
    ngOnInit() {
        this.selectOutStock();
    }
    // 选择调出仓
    selectOutStock () {
        Services.wmsWarehouseManage.list(new WmsWarehouseManageCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                console.log(item);
                this.extend.domain.outWarehouseName.items = [];
                item.page.content.forEach(wmsWarehouseManage => {
                    this.extend.domain.outWarehouseName.items.push(
                        {id: wmsWarehouseManage.uuid, name: wmsWarehouseManage.serName}
                    );
                });
            }
        });
    }
   /* // 搜索商品
    searchGoods() {
        Services.baseGoods.list(this.condition, {
            page: 0,
            size: 100,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(data => {
            this.goodsData = data;
            this.searchFlag = true;
        });
    }*/
    // 模拟搜索商品的数据
    addGooBaseGoodsSpecification () {
        const wmsStockDetail = new WmsStockDetail();
        wmsStockDetail.serItemUuid = '071cba55-e959-4fc0-848e-6fb10964a1ee';
        wmsStockDetail.serItemName = '华兰仕手表';
        wmsStockDetail.serSpecificationName = '规格名称';
        wmsStockDetail.serSpecificationUuid = '1ef0b265-4e3a-40ac-9743-abf69ad832de';
        wmsStockDetail.count = 0;
        wmsStockDetail.serItemImg = '商品图片';
        wmsStockDetail.serItemSerNum = '1';
        wmsStockDetail.serItemBrandUuid = '31fb2d68-c167-4ace-8b43-8c6cd6586cd5';
        wmsStockDetail.serItemBrandName = '品牌名称';
        wmsStockDetail.wmsStockType = 'STOCK_TRANSFER';
        console.log(wmsStockDetail);
        this.wmsStockDetailMap.set(Math.random().toString(), wmsStockDetail);
    }
    // 选择商品
    doSelectItem(data) {
        this.selectedGoods = data;
        // 查询商品对应的规格
        this.guiGeCondition.fkGooBaseGoodsUuid = data.uuid;
        Services.gooBaseGoodsSpecification.findByFkGooBaseGoodsUuid(this.guiGeCondition).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.specTypes = [];
                item.domains.forEach(gooBaseGoodsSpecification => {
                    this.specTypes.push(
                        {id: gooBaseGoodsSpecification.uuid, name: gooBaseGoodsSpecification.serName}
                    );
                });
            }
        });
        this.selectedGuiGe = null;
        this.specTypes = [];
        this.formGuiGe = [];
    }
    // 保存
    doSave() {
        if (!this.selectedGoods) {
            return Zero3Alert.req('请选择商品');
        }
        if (!this.selectedGuiGe) {
            return Zero3Alert.req('请选择商品规格');
        }
        this.saveOrder.emit(this.utils.buildGoodsData(this.selectedGoods, this.selectedGuiGe));
        this.lgModal.hide();
        this.clearForm();
    }

    dealGuiGeChange(item) {
        this.selectedGuiGe = item;
    }

    // 清除表单数据
    clearForm() {
        this.selectedGuiGe = null;
        this.specTypes = [];
        this.goodsData = null;
        this.selectedGoods = null;
        this.formGuiGe = [];
    }

    closeMod() {
        this.clearForm();
        this.lgModal.hide();
    }

}
